package com.example.keli.ui.home.fragment;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.keli.R;
import com.example.keli.api.HttpUtils;
import com.example.keli.api.RetrofitManager;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;
import com.example.keli.base.BaseFragment;
import com.example.keli.ui.area.AreaVO;
import com.example.keli.ui.home.activity.ObjectDetailActivity;
import com.example.keli.ui.home.adapter.HomeListAdapter;
import com.example.keli.ui.home.model.RecommendData;
import com.example.keli.ui.home.model.SexVo;
import com.kennyc.view.MultiStateView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * description:
 * author: Allen
 * date: 2019/3/18 16:34
 */
public class HomeListFragment extends BaseFragment {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;
    @BindView(R.id.refreshlayout)
    SwipeRefreshLayout mRefreshlayout;
    @BindView(R.id.multiStateView)
    MultiStateView multiStateView;

    HomeListAdapter adapter;
    List<RecommendData.DataBean> mListData = new ArrayList<>();

    String id = "";
    int page = 1;
    String sex = "2";//1男，2女
    String region_id = "";

    @Override
    protected int layoutId() {
        return R.layout.fragment_list_home;
    }

    @Override
    protected void bindData() {
        id = getArguments().getString(HomeFragment.FRAGMENT_TYPE_ID);
        adapter = new HomeListAdapter(mListData, getActivity());
        mRecyclerview.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        mRecyclerview.setAdapter(adapter);
        multiStateView.setViewState(MultiStateView.VIEW_STATE_LOADING);
        loadDatas();
    }

    @Override
    protected void bindListener() {
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadDatas();
            }
        }, mRecyclerview);
        mRefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mListData.clear();
                loadDatas();
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ObjectDetailActivity.start(getActivity(),mListData.get(position).getUid(),mListData.get(position).getAaid());
            }
        });
        EventBus.getDefault().register(this);
    }

    private void loadDatas() {
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        map.put("sex", sex);
        if(!TextUtils.isEmpty(region_id)){
            map.put("region_id", region_id);
        }
        map.put("page", String.valueOf(page));
        HttpUtils.ApiFunc(RetrofitManager.mApiService.getActivityUser(map), new SubscriberListener<BaseResponse<RecommendData>>() {
            @Override
            public void onSuccess(BaseResponse<RecommendData> response) {
                if (page == 1) {
                    mRefreshlayout.setRefreshing(false);
                    if(response.getData().getData().size()==0){
                        multiStateView.setViewState(MultiStateView.VIEW_STATE_EMPTY);
                    }else{
                        multiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                    }
                }
                if (response.getData().getData().size() == 0) {
                    adapter.loadMoreEnd();
                    return;
                }
                mListData.addAll(response.getData().getData());
                adapter.notifyDataSetChanged();
                adapter.loadMoreComplete();
            }

            @Override
            public void onFailer(String msg) {

            }

            @Override
            public void onTokenError() {

            }
        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(SexVo sexVo) {
        //切换性别，更新数据
        sex = sexVo.getSex();
        page = 1;
        mListData.clear();
        adapter.notifyDataSetChanged();
        loadDatas();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onChangedCity(AreaVO event) {
        //切换城市，刷新数据
        if (null != event) {
            if(TextUtils.equals(event.getName(),"全国")){
                region_id = "";
            }else{
                region_id = String.valueOf(event.getId());
            }
            page = 1;
            mListData.clear();
            adapter.notifyDataSetChanged();
            loadDatas();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
